Practice Problems and Solutions

You should do these problems on paper or mentally first, and only THEN check the solution.

 

Context-free grammars, parse trees, and ambiguity.

For the following languages, give the strings indicated. You may leave off quote marks for simplicity. The ordering of the strings is just to simplify checking solutions, and is not an essential part of the problem (don't fret about it).

7.1. The language { ak b ak | for k ≥ 1 } (just give the first 5 strings).

Show Solution

7.2. The language generated by the grammar (list all strings of length ≤ 5):

	S := a S a 
	S := b S b
	S := c
	S := d
Show Solution

7.3. The language generated by the grammar (list all strings of length ≤ 4):

	S := A B 
	A := a A
	A := c
	B := B b
	B := d
	
Show Solution

7.4. The language generated by the grammar (list all strings of length ≤ 5):

	S := A  
	A := a A a
	A := B
	B := b B b
	B := c
	
Show Solution

7.5. The language generated by the grammar (list all strings of length ≤ 7):

	S := A 
	S := B 
	A := a A bb
	A := a
	B := aa B b
	B := b
	
Show Solution

Problem 7.6 -- 7.9 are not relevant for Midterm 2 in Spring 2019.

7.6. Give a CFG corresponding to the regular expression (a (bb|cc) b)* a

Show Solution

7.7. For which of the regular expressions given in Part 1 would it be possible to construct a CFG generating the same language?

Show Solution

7.8. Give a regular expression representing the same language as the CFG in problem 2.3.

Show Solution

7.9. For which of the grammars in problems 2.2 - 2.5 would it be possible to find a regular expression representing the same language?

Show Solution

The following problem is designed to go with material on finite-state automata, but can be solved without it!

7.10. Give a CFG generating the language over {a,b}* with an even number of a's and an odd number of b's Hint: draw an automaton first, then write a grammar which has a non-terminal for each state; you may need to use erasing rules of the form A := ε.

Show Solution

For the following grammars and string s, give (a) a parse tree (b) a left-most derivation, and (c) a right-most derivation.

7.11.

	S := A B 
	A := a A
	A := c
	B := B b
	B := d
	

String s = aacdb

Show Solution

7.12.

	S := E
	E := a
	E := [ L ] 
	E := []
	L := E , L
	L := E
	

String s = [ a, [ a ] ]

Show Solution

7.13.

    S := E
    E := E + T 
    E := T 
    T := T * F 
    T := F 
    F := - F 
    F := F1 
    F1 := F2 ** F1
    F1 := F2
    F2 := a
	

String s = a + - a ** a

Show Solution

7.14.

    S := E
    E := E + T 
    E := T 
    T := T * F 
    T := F 
    F := - F 
    F := a
	

String s = a * a + -a

Show Solution

For the following CFGs, state whether or not they are ambiguous, and if ambiguous, give two different left-most derivations for some string OR two different parse trees.

7.15.

    S := A
    A := a A
    A := B
    B := a B
    B := a
Show Solution

7.16.

    S := A
    A := a A b
    A := a A
    A := b
Show Solution

7.17. (This grammar attempts to generate the language with an equal number of a's and b's.)

    S := A                     
    A := a A b
    A := b A a
    A := A A
    A := a b
    A := b a 

Show Solution

7.18. (The language of matching parentheses.)

    S := A                     
    A := ( A )
    A := A A
    A := ()

Show Solution

Answer the following two questions.

7.19. Write a CFG that represents the same language as the CFG in problem 7.15, but is NOT ambiguous.

Show Solution

7.20. Write a CFG that represents the same language as the CFG in problem 7.18, but is NOT ambiguous.

Show Solution